package com.company.gxcpc2020;

import java.util.Scanner;

public class GxcpcF {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-- > 0) {
			int x = sc.nextInt();
			x++;
			StringBuilder str = new StringBuilder(Integer.toString(x));
			int index = str.indexOf("7");
			if (index != -1) {
				str.setCharAt(index, '8');
			}
			x = check(Integer.parseInt(str.toString()));
			System.out.println(x);
			
		}
	}

	public static int check(int x) {
		return x % 7 == 0 ? x + 1 : x;
	}
}
